using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.EnterpriseFramework;
using System.Xml;

namespace CodePlex.EnterpriseFramework
{
    public class SharepointLists
    {
        // Private members
        private XmlDocument _batch;
        private string _listName;
        private string _url;
        private SPAttachmentProperties[] _attachements;


        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="list">Update list entity object</param>
        public SharepointLists(UpdateList list)
        {
            _batch = GetBatchString(list);
            _url = list.SharepointSiteUrl;
            _listName = list.ListName;
            _attachements = GetAttachementList(list);
        }

        // Public properties

        public XmlDocument Batch
        {
            get{ return _batch; }
        }

        public string ListName
        {
            get { return _listName; }
        }

        public string Url
        {
            get { return _url; }
        }

        public SPAttachmentProperties[] Attachments
        {
            get { return _attachements; }
        }


        
        /// <summary>
        /// This method is used to create standard sharepoint update xml data.
        /// 
        /// As an example of data: <code><Batch OnError='Continue' ListVersion='1'><Method ID='1' Cmd='New'><Field Name='Department'>BizTalk</Field><Field Name='FirstName'>Firas</Field><Field Name='Title'>Ammouri</Field><Field Name='FullName'>Firas A. Ammouri[Test]</Field><Field Name='HRRecruitRequisition'>1</Field><Field Name='Email'>fammouri@hotmail.com</Field><Field Name='HomePhone'>+962-79-6321.567</Field><Field Name='WorkAddress'>Amman, Sport City</Field><Field Name='WorkCity'>Amman</Field><Field Name='WorkCountry'>Jordan</Field><Field Name='AssignedTo'>153</Field><Field Name='HRRecruitReferralType'>Google Earth</Field><Field Name='Status'>New</Field></Method></Batch></code>
        /// </summary>
        /// <param name="list">Update list entity object</param>
        /// <returns>Xml data that represent the standrd format of sharepoint to update the list</returns>
        private XmlDocument GetBatchString(UpdateList list)
        {
            // Dclare general attibute obj
            XmlAttribute xmlAtt;

            // Create Batch document
            XmlDocument xmlBatchDoc = new XmlDocument();
            // Create Batch Node
            XmlNode xmlBatchNode = xmlBatchDoc.CreateNode(XmlNodeType.Element, "Batch", null);
            // Create batch attributes
            xmlAtt = xmlBatchDoc.CreateAttribute("OnError");
            xmlAtt.InnerText = "Continue";
            xmlBatchNode.Attributes.Append(xmlAtt);
            xmlAtt = xmlBatchDoc.CreateAttribute("ListVersion");
            xmlAtt.InnerText = "1";
            xmlBatchNode.Attributes.Append(xmlAtt);
            
            // Create Method Node
            XmlNode methodNode = xmlBatchDoc.CreateNode(XmlNodeType.Element, "Method", null);
            // Create batch attributes
            xmlAtt = xmlBatchDoc.CreateAttribute("ID");
            xmlAtt.InnerText = "1";
            methodNode.Attributes.Append(xmlAtt);
            xmlAtt = xmlBatchDoc.CreateAttribute("Cmd");
            xmlAtt.InnerText = "New";
            methodNode.Attributes.Append(xmlAtt);

            // Create Fields
            foreach (UpdateListAdd add in list.Fields)
            {
                // Create Field node
                XmlNode addNode = xmlBatchDoc.CreateNode(XmlNodeType.Element, "Field", null);
                addNode.InnerText = add.Value;
                // Add attributes
                xmlAtt = xmlBatchDoc.CreateAttribute("Name");
                xmlAtt.InnerText = add.name;
                addNode.Attributes.Append(xmlAtt);
                // Append newly created filed into Methods node
                methodNode.AppendChild(addNode);
            }

            // Append <Method> node
            xmlBatchNode.AppendChild(methodNode);
            // Append <Barch> node
            xmlBatchDoc.AppendChild(xmlBatchNode);
            return xmlBatchDoc;
        }

        
        /// <summary>
        /// Get attachements into string array
        /// </summary>
        /// <param name="list">Update List object</param>
        /// <returns>Array list of physical file locations</returns>
        private SPAttachmentProperties[] GetAttachementList(UpdateList list)
        {
            // Declare attachement array
            SPAttachmentProperties[] attachments = new SPAttachmentProperties[list.Attachments.Length];
            // Loop attachements
            for( int count = 0; count <list.Attachments.Length; count++)
            {
                attachments[count].FilePath = list.Attachments[count].Value;
                attachments[count].FileName = list.Attachments[count].name;
                attachments[count].Delete = list.Attachments[count].delete;
            }
            // return
            return attachments;
        }

        public struct SPAttachmentProperties
        {
            public string FileName;
            public string FilePath;
            public bool Delete;
        }
    }
}
